package com.relife.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 
 * @author weiwu
 * 
 */
public class DateUtil {

	protected static final Logger logger = Logger.getLogger(DateUtil.class);
	public static String YEAR = "yyyy";
	public static String MONTH = "MM";
	public static String DAY = "DD";
	/** yyyy-MM */
	public static String YEAR_MONTH = "yyyy-MM";
	public static String DATE = "yyyy-MM-dd";
	public static String DATE_CHINA = "yyyy年MM月dd日";
	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static String YYYYMMDD = "yyyyMMdd";
	public static String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
	public static String TIME = "HH:mm:ss";

	/**
	 * 
	 * @author WU
	 * @date 2015年9月7日 下午1:52:02
	 * @param sdf
	 * @return
	 */
	public static String getTime(String sdf) {
		SimpleDateFormat sd = new SimpleDateFormat(sdf);
		return sd.format(new Date());
	}

	/**
	 * 
	 * @author WU
	 * @date 2015年9月7日 下午1:52:08
	 * @param sdf
	 * @param date
	 * @return
	 */
	public static String getTime(String sdf, Date date) {
		SimpleDateFormat sd = new SimpleDateFormat(sdf);
		return sd.format(date);
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年8月12日 上午9:43:07
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 
	 * @author WU
	 * @date 2015年9月7日 下午1:53:07
	 * @param sdf
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getTime(String sdf, String date) {
		SimpleDateFormat sd = new SimpleDateFormat(sdf);
		try {
			return sd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/***
	 * 判断是否是周末(星期五,星期六)
	 * 
	 * @author WU
	 * @date 2016年7月4日 上午8:45:25
	 * @param date
	 * @return
	 */
	public static boolean isWeekend(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY;
	}

	/**
	 * 计算两个日期最之间的天数之差
	 * 
	 * @author WU
	 * @date 2016年7月7日 上午9:54:34
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int daysBetween(Date startDate, Date endDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(endDate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 获取当年的第一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}

	/**
	 * 获取当年的最后一天
	 * 
	 * @param year
	 * @return
	 */
	public static Date getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearLast(currentYear);
	}

	/**
	 * 获取某年第一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}
	

	/**
	 * 获取某年最后一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();

		return currYearLast;
	}

	
	/**
	 * 获取开始日期与结束日期之间相隔几个月
	 * @author chenmingdong
	 * @date 2017年8月11日下午3:32:10
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int getMonthNumber(String beginDate, String endDate) {
		int num = 0 ;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		
		try {
			Date date1 = sdf.parse(beginDate);
			Date date2 = sdf.parse(endDate);
			
			Calendar currCal = Calendar.getInstance();
			currCal.setTime(date1);
			
			int year1 = currCal.get(Calendar.YEAR);
			int month1 = currCal.get(Calendar.MONTH);
			
			currCal.setTime(date2);
			
			int year2 = currCal.get(Calendar.YEAR);
			int month2 = currCal.get(Calendar.MONTH);
			
			if(year2 > year1) {
				num += (year2 - year1) * 12;
			}
			
			if(month1 > month2) {
				num += 12 - month1 + month2;
			}else {
				num += month2-month1+1;
			}
			
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return num;
	}
	
	
	/**
	 * 获取当前月的第一天的时间戳值
	 * @author chenmingdong
	 * @date 2017年8月14日上午11:34:14
	 * @return
	 */
	public static Date getNowMonthFirst() {
		Calendar currCal = Calendar.getInstance();
		currCal.setTime(new Date());
		currCal.set(Calendar.DAY_OF_MONTH, currCal.getActualMinimum(Calendar.DAY_OF_MONTH));
		Date currMonthFirst = currCal.getTime();
		
		return currMonthFirst;
	}
	
	/**
	 * 获取当前月的最后一天的时间戳值
	 * @author chenmingdong
	 * @date 2017年8月14日上午11:34:14
	 * @return
	 */
	public static Date getNowMonthLast() {
		Calendar currCal = Calendar.getInstance();
		currCal.setTime(new Date());
		currCal.set(Calendar.DAY_OF_MONTH, currCal.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date currMonthLast = currCal.getTime();
		
		return currMonthLast;
	}
	
	
	/**
	 * 获取输入月份的第一天
	 * @author chenmingdong
	 * @date 2017年8月10日下午4:57:53
	 * @param dateStr
	 * @return
	 */
	public static Date getMonthFirst(String dateStr) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		 try {
			Date date = sdf.parse(dateStr);
			
			Calendar currCal = Calendar.getInstance();
			currCal.setTime(date);
			currCal.set(Calendar.DAY_OF_MONTH, currCal.getActualMinimum(Calendar.DAY_OF_MONTH));
			Date currMonthFirst = currCal.getTime();
			
			return currMonthFirst;
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 
		 return null;
		
	}
	
	/**
	 * 获取指定日期 num后个月的第一天日期
	 * @author chenmingdong
	 * @date 2017年8月17日下午3:11:42
	 * @param dateStr
	 * @param num
	 * @return
	 */
	public static Date getMonthFirst(String dateStr , int num) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		 try {
			Date date = sdf.parse(dateStr);
			
			Calendar currCal = Calendar.getInstance();
			currCal.setTime(date);
			currCal.set(Calendar.MONTH, currCal.get(Calendar.MONTH) + num);
			currCal.set(Calendar.DAY_OF_MONTH, currCal.getActualMinimum(Calendar.DAY_OF_MONTH));
			Date currMonthFirst = currCal.getTime();
			
			return currMonthFirst;
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 
		 return null;
		
	}
	
	/**
	 * 获取输入月份的最后一天
	 * @author chenmingdong
	 * @date 2017年8月10日下午4:57:53
	 * @param dateStr
	 * @return
	 */
	public static Date getMonthLast(String dateStr) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		
		 try {
			 
			Date date = sdf.parse(dateStr);
			
			Calendar currCal = Calendar.getInstance();
			currCal.setTime(date);
			currCal.set(Calendar.DAY_OF_MONTH, currCal.getActualMaximum(Calendar.DAY_OF_MONTH));
			Date currMonthLast = currCal.getTime();
			return currMonthLast;
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 
		 return null;
		
	}
	
	
	/**
	 * 获取指定日期 num后个月的最后一天日期
	 * @author chenmingdong
	 * @date 2017年8月17日下午3:13:36
	 * @param dateStr
	 * @param num
	 * @return
	 */
	public static Date getMonthLast(String dateStr , int num) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		 try {
			Date date = sdf.parse(dateStr);
			
			Calendar currCal = Calendar.getInstance();
			currCal.setTime(date);
			currCal.set(Calendar.MONTH, currCal.get(Calendar.MONTH) + num);
			currCal.set(Calendar.DAY_OF_MONTH, currCal.getActualMaximum(Calendar.DAY_OF_MONTH));
			Date currMonthFirst = currCal.getTime();
			
			return currMonthFirst;
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 
		 return null;
		
	}
	
	
	/**
	 * 获取当前月是第几个月
	 * @author chenmingdong
	 * @date 2017年8月11日下午3:38:49
	 * @return
	 */
	public static int getNowMonthNumber() {
		
		Date date = new Date();
		
		Calendar currCal = Calendar.getInstance();
		
		currCal.setTime(date);
		
		int month = currCal.get(Calendar.MONTH);
			
		return month;
	}
	
	
	public static void main(String[] args) {
		System.out.println(DateUtil.getYearFirst(2016));
	}
}
